package com.ruoyi.mq.service.serviceImpl.Processor;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.mq.service.MessageProcessor;
import com.ruoyi.system.domain.GwHomeSummarize;
import com.ruoyi.system.service.IGwHomeSummarizeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * 处理解析量
 */
@Component
public class ParseVolumeProcessor implements MessageProcessor {
    @Autowired
    private IGwHomeSummarizeService gwHomeSummarizeService;
    public void processMessage(JSONObject message) {
        System.out.println(message);
        // 处理发码量
        // 创建 ObjectMapper 实例
        // 转换为自定义对象
        String niot_id = message.getString("niot");
        //查询首页汇总数据
//        GwHomeSummarize gwHomeSummarize = gwHomeSummarizeService.selectGwHomeSummarizeByNodeIdentifier(niot_id);
//        //如果为空则新增，有数据则更新
//        if (gwHomeSummarize==null){
            GwHomeSummarize gwSummarize =new GwHomeSummarize();
            //设置节点标识
            gwSummarize.setNodeIdentifier(niot_id);
            gwSummarize.setAnalysisQuantity(Long.valueOf(message.getString("data")));
            gwSummarize.setCreateTime(new Date());
            //新增数据
            gwHomeSummarizeService.insertGwHomeSummarize(gwSummarize);
//        }else {
//            //判断解析量是否为空，不为空解析量相加
//            if (gwHomeSummarize.getAnalysisQuantity()==null){
//                gwHomeSummarize.setAnalysisQuantity(Long.valueOf(message.getString("data")));
//            }else {
//                gwHomeSummarize.setAnalysisQuantity(gwHomeSummarize.getAnalysisQuantity()+Long.valueOf(message.getString("data")));
//            }
//            //更新数据
//            gwHomeSummarizeService.updateGwHomeSummarize(gwHomeSummarize);
//        }


        System.out.println("处理解析量"+message);
    }
}
